<#assign base=request.contextPath />
<!DOCTYPE html>
<div class="row">
	<div class="col-md-12 col-sm-12 col-xs-12">
		<div class="x_panel">
			<div class="x_title">
				<h2>资讯编辑 <small></small></h2>
				<div class="clearfix"></div>
			</div>
			<div class="x_content">
				<br/>
				<form id="postForm" data-parsley-validate class="form-horizontal form-label-left" method="post" action="${base}/gx/sysShopping/save">
					<input type="hidden" id="id" name="id" value="${((obj.id)?c)!0 }">
		            <input type="hidden" name="addTime" value="${(obj.addTime)!.now?string['yyyy-MM-dd HH:mm:ss']}">
		            <input type="hidden" name="isDelete" value="${((obj.isDelete)?c)!0 }">
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">产品名称 <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<input type="text" id="commodityName" name="commodityName" value="${(obj.commodityName)!''}" class="form-control col-md-7 col-xs-12">
                        </div>
					</div>
		
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">产品分类 <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<select id="commodityType" name="commodityType" class="form-control select2">
                        		<option value="">请选择</option>
	                        		<option value="1">蔬菜</option>
		                        	<option value="2">水产</option>
		                        	<option value="3">家禽</option>
		                        	<option value="4">水果</option>
	                    	</select>
                        </div>
					</div>
				
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">产品图片 <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<input type="file" id="icon_dropify" name="file" class="dropify" data-default-file="${imagePath}${(obj.commodityImg)!''}" data-max-file-size="1M"/>
                            <input type="hidden" name="commodityImg" id="commodityImg" value="${(obj.commodityImg)!''}">
                            <input type="hidden" name="commodityImgOld" id="commodityImgOld" value="${(obj.commodityImg)!''}">
                        </div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">产品单价 <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<input type="text" id="commodityPrice" name="commodityPrice" value="${(obj.commodityPrice)!''}" class="form-control col-md-7 col-xs-12">
                        </div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">数量<span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<input type="text" id="commodityStock" name="commodityStock" value="${(obj.commodityStock)!''}" class="form-control col-md-7 col-xs-12">
                        </div>
					</div>
	
					<div class="ln_solid"></div>
					<div class="form-group">
						<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
							<button type="button" class="btn btn-success" id="submit">保存</button>
							<button type="button" class="btn btn-primary" id="goBack">返回</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
$(function() {
	//select2
	$(".select2").select2({width: "100%"});
	
	 $('.dropify').dropify({
     	messages: {
		        'default': '点击或拖拽文件到这里',
		        'replace': '点击或拖拽文件到这里来替换文件',
		        'remove':  '移除文件',
		        'error':   '对不起，你上传的文件太大了',
		    }
     });
     // Used events
     drEvent = $('#icon_dropify').dropify();
     drEvent.on('dropify.afterClear', function(event, element) {
     	$("#commodityImg").val("");
     });

     drEvent.on('dropify.errors', function(event, element) {
         console.log('Has Errors');
     });

	$("#goBack").click(function() {
		$("#main_content").load('${base}/gx/sysShopping/list');	        
    });
	
	$("#submit").click(function() {
	
		var commodityName = $.trim($("#commodityName").val());
		var commodityType = $.trim($("#commodityType").val());
		var commodityPrice = $.trim($("#commodityPrice").val());
		var commodityStock = $.trim($("#commodityStock").val());
		
    	if(commodityName==""){
    		swal("请输入产品名称");
            return;
    	}
    	if(commodityType==""){
    		swal("请选择分类");
            return;
    	}
    	//获取数据
		drEvent = $('.dropify').dropify();
	    drEvent=drEvent.data('dropify')
	    if((drEvent.file.object !=null )){
	    	var commodityImg = drEvent.callBackBase64;
	    	commodityImg = commodityImg.substring(commodityImg.indexOf("base64")+7);
	    	$("#commodityImg").val(commodityImg);
	    }else{
	    	if($("#commodityImg").val()==""){
	    		swal("请上传产品图片");
	            return;
	    	}
	    }
    	if(commodityPrice==""){
    		swal("请输入产品单价");
            return;
    	}
    	if(commodityStock==""){
    		swal("请输入产品数量");
            return;
    	}
    	

        $("#postForm").ajaxSubmit(function(data) {
    	 	if (data.flag) {
    	 		swal(data.msg, "", "success");
				$("#main_content").load('${base}/gx/sysShopping/list');
    	 	} 
    	 	else {
    	 		swal(data.msg, "", "error");
    	 	}
    	});
	});
});

</script>